package com.hibase.upms.controller;

import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import com.hibase.core.web.entity.OperationIdsEntity;
import com.hibase.upms.entity.dict.vo.MdsDictSearchVO;
import com.hibase.upms.entity.dict.vo.MdsDictVO;
import com.hibase.upms.entity.dictdata.MdsDictData;
import com.hibase.upms.entity.dictdata.vo.*;
import com.hibase.upms.service.dictdata.MdsDictDataService;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author chenfeng
 * @since 2021-03-12
 */
@RestController
@RequestMapping("/mds-dict-data")
@Api(tags = "字典表")
public class MdsDictDataController{

    @Autowired
    private MdsDictDataService mdsDictDataService;


    @ApiOperation(value = "查询字典表列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectMdsDictDataList")
    public ResponseModel<MdsDictDataListVO> selectMdsDictDataList(@RequestBody MdsDictDataSearchVO searchVO) {

        PageInfo result = mdsDictDataService.selectMdsDictDataList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), MdsDictDataListVO.class));

        return ResponseModel.successMsg(result);
    }


    @ApiOperation(value = "新增字典表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertMdsDictData")
    public ResponseModel<String> insertMdsDictData(@RequestBody MdsDictDataInsertVO insertVO) {
    	if (StrUtil.isNotBlank(insertVO.getParentId()) && StrUtil.isBlank(insertVO.getDictId())) {
    		insertVO.setDictId(insertVO.getParentId());
		}
        MdsDictData entity = EntityUtil.transform(insertVO, MdsDictData.class);

        mdsDictDataService.saveMdsDictData(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "修改字典表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updateMdsDictData")
    public ResponseModel<String> updateMdsDictData(@RequestBody MdsDictDataUpdateVO updateVO) {

        MdsDictData entity = EntityUtil.transform(updateVO, MdsDictData.class);

        mdsDictDataService.saveMdsDictData(entity);

        return ResponseModel.successMsg(entity.getId());
    }


    @ApiOperation(value = "查询字典表详情", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewMdsDictDataDetails")
    public ResponseModel<MdsDictDataViewVO> viewMdsDictDataDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(EntityUtil.transform(mdsDictDataService.viewMdsDictData(operationidEntity.getId()), MdsDictDataViewVO.class));
    }


    @ApiOperation(value = "删除字典表记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deleteMdsDictData")
    public ResponseModel<String> deleteMdsDictData(@RequestBody OperationIdEntity delete) {

        mdsDictDataService.deleteMdsDictData(delete.getId());

        return ResponseModel.successMsg();
    }


    @ApiOperation(value = "查询字典数据项(选择框)", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectDictByCode")
    public ResponseModel selectDictByCode(@RequestBody MdsDictSearchVO dictSearchVO) {
        List<MdsDictData> result = mdsDictDataService.selectDictByCode(dictSearchVO.getCode());
        return ResponseModel.successMsg(EntityUtil.transform(result, MdsDictVO.class));
    }


    @ApiOperation(value = "启用字典数据项", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/enableMdsDictData")
    public ResponseModel<String> enableMdsDictData(@RequestBody OperationIdsEntity ids) {

        mdsDictDataService.enableMdsDictData(ids.getIds());

        return ResponseModel.successMsg();
    }


    @ApiOperation(value = "禁用字典数据项", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/disableMdsDictData")
    public ResponseModel<String> disableMdsDictData(@RequestBody OperationIdsEntity ids) {

        mdsDictDataService.disableMdsDictData(ids.getIds());

        return ResponseModel.successMsg();
    }
}
